/*
 * RegistrationAction.java
 *
 * Created on 8 2007, 20:58
 */

package server.reqistration;

import com.persistent.entity.Account;
import com.persistent.entity.Tank;
import com.persistent.model.facade.AccountFacade;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
/**
 *
 * @author alexog
 * @version
 */

public class RegistrationAction extends Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    private static final Logger log;
        
    static {
        log = Logger.getLogger(server.authentification.module.actions.LoginPage.class);
    }
    
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        RegistrationForm regForm = (RegistrationForm) form;
        
        AccountFacade accFacade = new AccountFacade();
        accFacade.setUserPassword(regForm.getPassword());
        
        Account account = new Account();
        account.setUsername(regForm.getUsername());
        account.setFirstname(regForm.getFirstName());
        account.setLastname(regForm.getLastName());
        account.setEmail(regForm.getLastName());
        account.setSecurityQuestion(regForm.getSeqQuestion());
        account.setSecurityAnswer(regForm.getSeqAnswer());
        account.setLocale(request.getLocale());
        account.setTankCollection(new ArrayList<Tank>());        
        accFacade.create(account);
        
        log.debug(accFacade.findAll());
        accFacade.close();
        return mapping.findForward(SUCCESS);
        
    }
}
